import './index.css'
import { getAntd,getEcCom,getLabel } from '../../util/convert';
import WeaWfInput from '../wf-form-input/index'
import * as _ from '../../util/public/common';
import FormSignInput from '../../asyncComps/FormSignInput';
import {toJS} from 'mobx';
import {needMustInputDot} from "../../util/signUtil";

class main extends React.Component {
    constructor(props) {
        super(props);
        const {markInfo={}}=this.props;
        const {workflowRequestLogId=""}=markInfo;
        this.state = {
            value: props.value ? props.value : '',
            AutographValue:workflowRequestLogId,
        };
    }
    componentWillMount() {
        const { fieldid,specialobj } = this.props;
        if(fieldid == -2)
            this.setState({leveldata: toJS(specialobj)});
    }
    componentWillReceiveProps(nextProps) {
        const {markInfo={}}=nextProps;
        const {workflowRequestLogId=""}=markInfo;
        const {markInfo:markInfo_last={}}=this.props;
        const {workflowRequestLogId:workflowRequestLogId_last=""}=markInfo_last;
        if (this.state.value !== nextProps.value) {
            this.setState({value: nextProps.value});
        }
        if(workflowRequestLogId != workflowRequestLogId_last){
            this.setState({AutographValue: workflowRequestLogId});
        }
    }
    getAutographValue = () =>{
        if(window.saveAutograph){
            let requestlog = window.saveAutograph(false)
            this.setState({AutographValue:requestlog})
        }
    }
    doChangeEvent = (e) => {
        this.setState({
            value: e.target.value
        });
        this.props.onChange && this.props.onChange(e.target.value);
    }


    doChangeValue = (val) => {
        this.setState({
            value: val
        });
        this.props.onChange && this.props.onChange(val);
    }

    doChangeValueEventck = (datas = {}, ischangeremark) => {
        console.log('test--123456')
        //WfForm.updateRemarkInfo(datas);
        if (ischangeremark) {
            let val = datas.remark;
            this.setState({
                value: val
            });
            this.props.onChange && this.props.onChange(val);
        }
    }

    changeRemarkInfo(params = {}) {
        const {changeSubmitParam} = this.props;
        changeSubmitParam && changeSubmitParam(params);
    }

    // shouldComponentUpdate(nextProps, nextState) {
    //     if (this.props.fieldid == -4) {
    //         return !_.isEqual(this.props.markInfo, nextProps.markInfo) ||
    //             this.props.requestType !== nextProps.requestType ||
    //             this.state.value !== nextState.value ||
    //             this.state.value !== nextProps.value;
    //     } else {
    //         return this.state.value !== nextProps.value ||
    //             this.props.viewAttr !== nextProps.viewAttr ||
    //             this.props.defaultFocus !== nextProps.defaultFocus;
    //     }
    // }

    /**
     * fieldid 值说明：
     * -1 ： 请求标题
     * -2：  紧急程度
     * -3：  短信提醒
     * -4：  签字意见
     * -5：  微信提醒
     * -9： 打印次数
     */

    render() {
        const {Radio} = getAntd();
        const { WeaSelect } = getEcCom();
        const RadioGroup = Radio.Group;
        let {value,AutographValue} = this.state;
        const {viewAttr, fieldid, fieldName, markInfo = {}, requestType, defaultFocus,containSignInput,secLevelInfo={}} = this.props;
        const isview = viewAttr === 1;
        let el;
        let req = "";
        let remarkDivStyle = {};
        if (fieldid == -1) {
            if (isview) {
                el = <span dangerouslySetInnerHTML={{__html: value}}></span>
            } else {
                return <WeaWfInput ecId={`${this && this.props && this.props.ecId || ''}_WeaWfInput@uup5ai`} viewAttr={viewAttr} fieldid="-1" fieldName={fieldName} isDetail={false}
                                   detailtype={1} format={{}}
                                   defaultFocus={defaultFocus} value={value}
                                   style={{'height': '100%', 'width': '90%'}}
                                   onChange={this.doChangeValue}/>
            }
        } else if (fieldid == -2) {
            const leveldata = this.state.leveldata;
            if (isview) {
                let item = leveldata.filter(itm => itm.id == value);
                try{
                    el = <span>{item[0].customname}</span>
                } catch (e) {
                }
            } else {
                let radioArr = [];
                leveldata && leveldata.map((item,index) =>{
                    radioArr.push(<Radio ecId={`${this && this.props && this.props.ecId || ''}_Radio@ive0mt@${index}`} value={item.id} >{item.customname}</Radio>);
                })
                el = <RadioGroup ecId={`${this && this.props && this.props.ecId || ''}_RadioGroup@pdngi5`} onChange={this.doChangeEvent} value={value}>{radioArr}</RadioGroup>
            }
        } else if (fieldid == -3) {
            value = value == '' ? '0' : value;
            const map3 = {'0': getLabel(17583,'不短信提醒'), '1': getLabel(17584,'离线短信提醒'), '2': getLabel(17585,'在线短信提醒')};
            if (isview) {
                el = <span>{map3[value]}</span>
            } else {
                let radioArr = [];
                _.forIn(map3, (v, k) => {
                    radioArr.push(<Radio ecId={`${this && this.props && this.props.ecId || ''}_Radio@wh3gq9@${k}`} value={k}>{v}</Radio>);
                });
                el = <RadioGroup ecId={`${this && this.props && this.props.ecId || ''}_RadioGroup@iknvn1`} onChange={this.doChangeEvent} value={value}>{radioArr}</RadioGroup>
            }
        } else if (fieldid == -4) {
            //签字意见相关
            const {hasLoadMarkInfo = false, remarkInfo = {}, isSignMustInput = '',signMustInputTypes = [], isFormSignature=""} = markInfo;
            const remarkMustInput = needMustInputDot();;
            if (requestType > 0 && hasLoadMarkInfo && containSignInput && this.props.JSComplete === true) {
                let requiredSpan;
                if (remarkMustInput) {
                    let _value = value;
                    _value = _value.replace(/&lt;/ig, "<");
                    _value = _value.replace(/&gt;/ig, ">");
                    _value = _value.replace(/<br>/ig, "");
                    _value = _value.replace(/<p>/ig, "").replace(/<\/p>/ig, "").replace(/ /ig, "").replace(/&nbsp;/ig, "").replace(/<br \/>/ig, "");

                    req = "wf-form-textarea";
                    remarkDivStyle['padding-right'] = "12px";
                    requiredSpan = <span className="wf-textarea-required">
                        {(_.trim(_value).length === 0 && isFormSignature != 1) ||(isFormSignature == 1 && (AutographValue==-3 || AutographValue==-2 || AutographValue==-1))? <img src="/images/BacoError_wev9.png" align="absmiddle" /> : ""}
                    </span>;
                }
                el = [<FormSignInput ecId={`${this && this.props && this.props.ecId || ''}_FormSignInput@23odbt@signinput`} isInForm = {true}  key="_formCellSignInput" onChangeEvent={this.doChangeValueEventck} onAutographBlur = {this.getAutographValue} />, requiredSpan]
            }
        } else if (fieldid == -5) {
            value = value == '' ? '0' : value;
            const map5 = {'0': getLabel(19782,'不提醒'), '1': getLabel(15148,'提醒')};
            if (isview) {
                el = <span>{map5[value]}</span>
            } else {
                let radioArr = [];
                _.forIn(map5, (v, k) => {
                    radioArr.push(<Radio ecId={`${this && this.props && this.props.ecId || ''}_Radio@a92u50@${k}`} value={k}>{v}</Radio>);
                });
                el = <RadioGroup ecId={`${this && this.props && this.props.ecId || ''}_RadioGroup@39gqxc`} onChange={this.doChangeEvent} value={value}>{radioArr}</RadioGroup>
            }
        } else if (fieldid == -9) {
            el = <span>{_.trim(value).length === 0 ? "0" : value}</span>
        } else if(fieldid == -10) {//密级字段
            if (isview) {
                el = <span>{secLevelInfo.secLevelName}</span>
            } else {
                el = <WeaSelect ecId={`${this && this.props && this.props.ecId || ''}_WeaSelect@gt5c9l`} viewAttr={secLevelInfo.canSecChange ? 3 : 1} onChange={this.doChangeValue} options={secLevelInfo.secLevelOption} value={secLevelInfo.secLevel}/>
            }
        }

        return (
            <div className={`wf-system-field ${req}`} style={remarkDivStyle}>
                {el}
                <input type="hidden" name={fieldName} id={fieldName} value={value}/>
            </div>
        )
    }
}

export default main
